package com.newy.dao.sys;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.newy.core.hibernate.HibernateDao;
import com.newy.domain.sys.Department;

/**
 * 
 * @author linzongxue
 *
 */
@Repository
public class DepartDao extends HibernateDao<Department, Long> {
	/**
	 * 删除时做删除标记，不允许直接删除
	 */
	public void delete(Department depart){
		if (depart == null) return;
		depart.setIsDeleted(true);
		this.save(depart);
	}
	
	/**
	 * 获取直接子部门
	 */
	public List<Department> getChildren(Long parentId){
		List<Criterion> criterions = new ArrayList<Criterion>(2);
		criterions.add(parentId == null ? Restrictions.isNull("parentId") : Restrictions.eq("parentId", parentId));
		criterions.add(Restrictions.eq("isDeleted", false));
		return find(criterions, this.ascSortOrder);
	}
	
	/**
	 * 获取指定部门的所有子部门
	 */
	public List<Department> getAllChildren(Long parentId){
		Department parent = this.get(parentId);
		return this.find(Restrictions.like("parentIdPath", parent.getParentIdPath() + parentId + "/%"), 
				Order.asc("parentIdPath"));
	}
}
